package com.rongji.dfish.demo.test.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.rongji.dfish.framework.plugin.file.service.FileService;
import com.rongji.dfish.framework.plugin.file.ui.DefaultUploadFile;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.form.Linkbox;
import com.rongji.dfish.ui.form.Onlinebox;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.helper.GridPanel;
import com.rongji.dfish.ui.helper.GridPanelTest.TestBean;
import com.rongji.dfish.ui.helper.GroupingGridPanel;
import com.rongji.dfish.ui.helper.TabPanel;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.layout.grid.GridColumn;
import com.rongji.dfish.ui.widget.Html;

public class DemoTestView {

	public static View buildIndexView() {
		View view = new View();
		
		TabPanel tab = new TabPanel(null);
		view.add(tab);
//		tab.setTabPosition(TabPanel.POS_LEFT);
//		tab.getTabBar().setDir(ButtonBar.DIR_VERTICAL).setWidth(200);
		tab.getTabBar().setHeight(40);
		tab.getTabBar().getPub().setTip(true);
		
		tab.add(buildFormPanel(), "FormPanel");
		tab.add(buildGridPanel(), "GridPanel");
		tab.add(buildGroupingGridPanel(), "GroupingGridPanel");
		
		return view;
	}
	
	public static Widget<?> buildGridPanel() {
		
//		VerticalLayout root = new VerticalLayout(null);
//		view.add(root);
		
//		GridLayoutFormPanel form = new GridLayoutFormPanel("");
//		root.add(form);
//		int row = 0;
//		int column = 0;
//		form.add(row, column++, new Text("test1", "测试1", "测试1"));
//		form.add(row, column++, new Text("test2", "测试2", "测试2"));
//		form.add(row, column++, new Text("test3", "测试3", "测试3"));
//		
////		form.getPrototype().getColumns().get(0).setWidth("200");
//		
//		row++;
//		column = 0;
//		form.getPrototype().add(row, column++, new Text("test4", "测试4", "测试4"));
//		form.getPrototype().add(row, column++, new Text("test5", "测试5", "测试5"));
//		form.getPrototype().add(row, column++, new Text("test6", "测试6", "测试6"));
//		
////		new GridLayout(null).toString();
////		new Html(null).toString();
//		
////		form.getPrototype().getColumns().get(2).setWidth("300");
//		
//		row++;
//		column = 0;
//		form.getPrototype().add(row, column++, new Text("test7", "测试7", "测试7"));
//		
////		form.getPrototype().getColumns().get(4).setWidth("400");
//		
//		row++;
//		column = 0;
//		form.getPrototype().add(row, column++, new Text("test11", "测试11", "测试11"));
		
		
//		GridLayout gridLayout=new GridLayout("f_form");
//		root.add(gridLayout);
//		gridLayout.setId("f_grid");
//		gridLayout.setFace(GridLayout.FACE_LINE);
//		
//		gridLayout.addColumn(GridColumn.rownum("序号", "80"));
//		gridLayout.addColumn(GridColumn.radio("n2", "40"));
//		gridLayout.addColumn(GridColumn.text("stat", "60").setAlign(GridColumn.ALIGN_CENTER));
//		gridLayout.addColumn(GridColumn.text("tm", "100"));
//		gridLayout.addColumn(GridColumn.text("n1", "*").setAlign(GridColumn.ALIGN_RIGHT));
//		gridLayout.addColumn(GridColumn.text("f1", "40").setAlign(GridColumn.ALIGN_CENTER));
//		gridLayout.addColumn(GridColumn.text("rs", "60").setAlign(GridColumn.ALIGN_CENTER));
//		gridLayout.addColumn(GridColumn.text("f2", "40").setAlign(GridColumn.ALIGN_CENTER));
//		gridLayout.addColumn(GridColumn.text("n2", "*").setAlign(GridColumn.ALIGN_LEFT));
//		gridLayout.addColumn(GridColumn.text("oper", "200").setFormat("<a href=#>$oper</a>"));
//
//		Tr headRow=new Tr().setHeight("40").setCls("bg-form bottom-line");
//		Thead thead = gridLayout.getThead();
//		gridLayout.getThead().add(headRow);
//		headRow.setData(GridColumn.FIELD_ROWNUM, "序号");
//		headRow.setData(GridColumn.FIELD_TRIPLEBOX, new RawJson(new Triplebox(GridColumn.BOX_NAME, null, null, null, null).setCheckall(true).toString()));
//		headRow.setData("stat", "状态");
//		headRow.setData("tm", "日期");
//		headRow.setData("n1", "队名");
//		headRow.setData("f1", "");
//		headRow.setData("rs", "结果");
//		headRow.setData("f2", "<a href=javascript:; onclick=$.alert('$f2')>测试</a>");
//		headRow.setData("n2", "队名");
//		headRow.setData("oper", "链接");
//		
////		gridLayout.setPub(new Tr().setHeight("32")
////				.setOn("click", "alert($result)"));
//		Object[][] DATA=new Object[][]{
//				{"e","06/13 04:00","巴西","img/fifa/bra.png","3 : 1","img/fifa/cro.png","克罗地亚","这里应该是链接位",1,new Date()},
//				{"e","06/14 00:00","墨西哥","img/fifa/mex.png","1 : 0","img/fifa/cmr.png","喀麦隆","这里应该是链接位",2,new Date()},
//				{"e","06/18 03:00","巴西","img/fifa/bra.png","0 : 0","img/fifa/mex.png","墨西哥","这里应该是链接位",3,new Date()},
//				{"e","06/19 06:00","喀麦隆","img/fifa/cmr.png","0 : 4","img/fifa/cro.png","克罗地亚","这里应该是链接位",4,new Date()},
//				{"e","06/24 04:00","喀麦隆","css/g/logo-white.png","1 : 4","img/fifa/bra.png","巴西","这里应该是链接位",5,new Date()},
//				{"e","06/24 04:00","克罗地亚","img/fifa/cro.png","1 : 3","img/fifa/mex.png","墨西哥","这里应该是链接位",6,new Date()}
//		};
//		for(Object[] r:DATA){
//			Tr contRow=new Tr();
//			gridLayout.add(contRow);
//			contRow.setData("stat", r[0]);
//			contRow.setData("tm", r[1]);
//			contRow.setData("n1", r[2]);
//			contRow.setData("f1", r[3]);
//			contRow.setData("rs", r[4]);
//			contRow.setData("f2", r[5]);
//			contRow.setData("n2", r[6]);
//			contRow.setData("oper", r[7]);
//			contRow.setData("id", r[8]);
//			contRow.setData("ot", r[9]);
////			System.out.println(contRow);
//		}
//		gridLayout.put(1, 2, 2, 2, "测试合并");
		
		
//		GridPanel grid = new GridPanel("f_grid");
//		root.add(grid);
//		
////		GridLayout layout = grid.getPrototype();
//		
////		layout.setPub(new Tr().setHeight("32")
////				.setOn("click", "alert($result)"));
////		grid.setHasTableHead(false);
//		Object[][] DATA=new Object[][]{
//				{"e20","06/13 04:00","巴西","img/fifa/bra.png","3 : 1","img/fifa/cro.png","克罗地亚","这里应该是链接位",1},
//				{"e21","06/14 00:00","墨西哥","img/fifa/mex.png","1 : 0","img/fifa/cmr.png","喀麦隆","这里应该是链接位",2},
//				{"e22","06/18 03:00","巴西","img/fifa/bra.png","0 : 0","img/fifa/mex.png","墨西哥","这里应该是链接位",3},
//				{"e23","06/19 06:00","喀麦隆","img/fifa/cmr.png","0 : 4","img/fifa/cro.png","克罗地亚","这里应该是链接位",4},
//				{"e24","06/24 04:00","喀麦隆","img/fifa/cmr.png","1 : 4","img/fifa/bra.png","巴西","这里应该是链接位",5},
//				{"e25","06/24 04:00","克罗地亚","img/fifa/cro.png","1 : 3","img/fifa/mex.png","墨西哥","这里应该是链接位",6}
//		};
//		grid.setFace(GridPanel.FACE_DOT);
//		
//		int dataColumnIndex = 0;
//		grid.addColumn(GridColumn.rownum("序号", "60"));
//		grid.addColumn(GridColumn.checkbox("f1", "40"));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "stat", "状态22", "60").setAlign(GridColumn.ALIGN_CENTER));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "tm", "日期2", "100"));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "n1", "队名2", "*").setAlign(GridColumn.ALIGN_CENTER));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "f1", "", "40").setAlign(GridColumn.ALIGN_CENTER));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "result", "结果2", "60").setAlign(GridColumn.ALIGN_CENTER));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "f2", "<a href=javascript:; onclick=alert(1);>测试2</a>", "40"));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "n2", "队名2", "*").setAlign(GridColumn.ALIGN_CENTER));
//		grid.addColumn(GridColumn.text(dataColumnIndex++, "oper", "链接2", "200").setFormat(new JSFunction("","var a=$oper; return a;")));
//		grid.addColumn(GridColumn.hidden(dataColumnIndex++, "id"));
//		grid.setGridData(Arrays.asList(DATA));

//		GridLayout gridLayout = new GridLayout("");

//		Thead thead = new Thead();
//		gridLayout.add(thead);

//		gridLayout.getThead().add(new Tr()
//		    .setHeight("40")
//		    .setCls("bg-form bottom-line")
//		    .setData("n1","队名")
//		    .setData("tm","日期")
//		    .setData("oper","链接")
//		    .setData("n2","队名")
//		    .setData("rs","结果")
//		    .setData("f1","")
//		    .setData("f2","<a href=javascript:; onclick=alert(1);>测试</a>")
//		    .setData("stat","状态"));
//
//		gridLayout.setPub(new Tr()
//		    .setHeight("32")
//		    .setCls("bg-form bottom-line")
//		    .setOn(Tr.EVENT_CLICK, "alert($result)"));
//
//		gridLayout.addColumn(new GridColumn()
//		    .setField("stat")
//		    .setWidth("60")
//		    .setAlign(GridColumn.ALIGN_CENTER));
//
//		gridLayout.addColumn(new GridColumn().setField("tm").setWidth("100"));
//
//		gridLayout.addColumn(new GridColumn().setField("n1").setAlign(GridColumn.ALIGN_RIGHT));
//
//		gridLayout.addColumn(new GridColumn()
//		    .setField("f1")
//		    .setWidth("40")
//		    .setAlign(GridColumn.ALIGN_CENTER));
//
//		gridLayout.addColumn(new GridColumn()
//		    .setField("rs")
//		    .setWidth("60")
//		    .setAlign(GridColumn.ALIGN_CENTER));
//
//		gridLayout.addColumn(new GridColumn()
//		    .setField("f2")
//		    .setWidth("40")
//		    .setAlign(GridColumn.ALIGN_CENTER));
//
//		gridLayout.addColumn(new GridColumn().setField("n2").setAlign(GridColumn.ALIGN_LEFT));
//
//		gridLayout.addColumn(new GridColumn()
//		    .setField("oper")
//		    .setWidth("200")
//		    .setFormat("<a href=#>$oper</a>"));
//
//		gridLayout.add(new Tr()
//		    .setData("n1","巴西")
//		    .setData("tm","06/13 04:00")
//		    .setData("oper","这里应该是链接位")
//		    .setData("n2","克罗地亚")
//		    .setData("rs","3 : 1")
//		    .setData("f1","img/fifa/bra.png")
//		    .setData("f2","img/fifa/cro.png")
//		    .setData("stat","e"));


		List<TestBean> dataList = new ArrayList<TestBean>();
		for (int i=1;i<=10;i++) {
			TestBean bean = new TestBean();
			bean.setId("id_" + i);
			bean.setName("名称_" + i);
			bean.setCount(i);
			bean.setValid((i % 2 == 0) ? true : false);
			bean.setCreateTime(new Date());
			dataList.add(bean);
		}
		
		GridPanel grid = new GridPanel(null);
//		view.add(grid);
		grid.setGridData(dataList);
		
		grid.addColumn(GridColumn.text("id", "id", "编号", "100").setAlign(GridColumn.ALIGN_CENTER));
		grid.addColumn(GridColumn.text("name", "n", "名称", "*").setFormat("<a href=javascript:; onclick=\"alert('$n');$e_cancel();\">$n</a>"));
		grid.addColumn(GridColumn.text("count", "c", "数量", "100").setDataFormat("#.000"));
		grid.addColumn(GridColumn.text("createTime", "ct", "创建时间", "130").setDataFormat("yyyy-MM-dd HH:mm"));
		
		
		grid.getPub().setHeight("32").setOn("click", "alert($id)");
		
		grid.setFace(GridPanel.FACE_LINE);
		
		return grid;
	}
	
	public static Widget<?> buildFormPanel() {
		FormPanel formPanel = new FormPanel(null).setHeight("600").setWidth("600");

		formPanel.add(new Onlinebox("Onlinebox", "在线匹配推荐列表", "", "sugguestShow/orgSuggest?keyword=$text&orgId=$value")
		    .setPlaceholder("请输入内容")
		    .setWidth("0200"));
		
		formPanel.add(new Linkbox("Linkbox", "下拉框", "", "sugguestShow/orgSuggest?keyword=$text&orgId=$value")
//	    	.setDropsrc("sugguestShow/orgSuggest?keyword=$text&orgId=$value")
		    .setPlaceholder("可输入的下拉框")
		    .setWidth("200")
		    .setSuggest(true));
		formPanel.add(new DefaultUploadFile("testFile", "附件测试", null));
		// 1br17meoqflt0000
		formPanel.add(new Html("<a href=javascript:; onclick=\"$.download('file/downloadFile?fileId=pat_oA5-ZtJt2QSaGXI9Rf7KQpTN_d7f')\">附件下载测试</a>"));
		
		return formPanel;
	}
	
	public static void main(String[] args) {
	    System.out.println(FileService.encId("1br17meoqflt0000"));
    }
	
	public static Widget<?> buildGroupingGridPanel() {
		GroupingGridPanel layout=new GroupingGridPanel(null);
//		view.add(layout);
		layout.setFace(GroupingGridPanel.FACE_CELL);
		layout.addColumn(GridColumn.text(0,"num","序号", "40").setAlign(GridColumn.ALIGN_RIGHT));
		layout.addColumn(GridColumn.text(1,"n","名称", "*"));
		layout.addColumn(GridColumn.text(2,"area","面积(平方公里)", "120"));
		layout.addColumn(GridColumn.text(3,"pop","人口(万人)", "120"));
		layout.addColumn(GridColumn.text(4,"postcode","邮编", "120"));
		
		
		String[][] FUZHOU_DATA={
				{"1","鼓楼区","74	","45.5","350033"},
				{"2","台江区","33","44.4","350034"},
				{"3","晋安区","123","56.7","350044"},
				{"4","马尾区","66	","43.2","350032"},
				{"5","仓山区","543","11.3","350131"},
			};
		layout.addGridData("福州市", Arrays.asList(FUZHOU_DATA));
			
		
		String[][] XIAMEN_DATA={
			{"1","思明区","74.66","46.88","你猜"},
			{"2","湖里区","60.71","13.88","你猜"},
			{"3","集美区","268","17.15","你猜"},
			{"4","海沧区","154.96","8.66","你猜"},
			{"5","同安区","657.59","39.40","你猜"},
			{"6","翔安区","351.64","25.79","你猜"},
		};
		layout.addGridData("厦门市", Arrays.asList(XIAMEN_DATA));
		return layout;
	}
	
}
